package com.yongbao.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yongbao.common.PageInfo;
import com.yongbao.common.utils.AssertUtil;
import com.yongbao.mapper.DishMapper;
import com.yongbao.mapper.DishOrderDishMapper;
import com.yongbao.model.domain.DishDO;
import com.yongbao.model.domain.DishOrderDishDO;
import com.yongbao.model.domain.DishSpecificationDO;
import com.yongbao.model.dtomapper.DishDtoMapper;
import com.yongbao.model.dtomapper.DishOrderDishDtoMapper;
import com.yongbao.model.request.DishOrderDishRequest;
import com.yongbao.model.util.DishConvert;
import com.yongbao.model.util.DishOrderDishConvert;
import com.yongbao.model.util.DishSpecificationConvert;
import com.yongbao.model.vo.DishOrderDishVO;
import com.yongbao.model.vo.DishVO;
import com.yongbao.service.DishOrderDishService;
import com.yongbao.service.DishSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class DishOrderDishServiceImpl extends ServiceImpl<DishOrderDishMapper, DishOrderDishDO> implements DishOrderDishService {
    @Autowired
    private DishOrderDishMapper dishOrderDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishSpecificationService dishSpecificationService;

    @Override
    public DishOrderDishVO getByKey(long id) {
        DishOrderDishVO dishOrderDishVO = DishOrderDishDtoMapper.INSTANCE.do2VO(getById(id));
        return dishOrderDishVO;
    }

    @Override
    public PageInfo<DishOrderDishVO> pageQuery(DishOrderDishRequest request) {
        long count = dishOrderDishMapper.pageCount(request);
        List<DishOrderDishDO> list = dishOrderDishMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<DishOrderDishVO> dishOrderDishVOList = DishOrderDishConvert.buildDishOrderDishVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), dishOrderDishVOList, count);
    }

    @Override
    public List<DishVO> listQuery(DishOrderDishRequest request) {
        List<DishOrderDishDO> list = dishOrderDishMapper.selectDOList(DishOrderDishDtoMapper.INSTANCE.request2DO(request));
        List<DishOrderDishVO> dishOrderDishVOS = DishOrderDishConvert.buildDishOrderDishVoList(list);
        List<DishVO> dishVOList = Lists.newArrayList();
        for (DishOrderDishVO dishOrderDishVO : dishOrderDishVOS) {
            DishDO dishDO = dishMapper.selectDOById(dishOrderDishVO.getDishId());
            if (dishDO != null) {
                DishVO dishVO = DishDtoMapper.INSTANCE.do2VO(dishDO);
                // 查询规格集合
                List<DishSpecificationDO> specList = dishSpecificationService.lambdaQuery().eq(DishSpecificationDO::getDishId, dishVO.getId()).list();
                dishVO.setSpecifications(DishSpecificationConvert.buildDishSpecificationVoList(specList));
                dishVOList.add(dishVO);
            }
        }
        return dishVOList;
    }

    @Override
    public Boolean add(DishOrderDishRequest request) {
        save(DishOrderDishDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean updateByKey(DishOrderDishRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(DishOrderDishDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean delByIds(String ids) {
        dishOrderDishMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
